package com.example.elysiumease.controller;

import com.example.elysiumease.exception.BusinessException;
import com.example.elysiumease.mapper.OperationTypeMapper;
import com.example.elysiumease.model.OperationType;
import com.example.elysiumease.result.ResponseResult;
import jakarta.validation.constraints.Min;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import lombok.extern.slf4j.Slf4j;

@RestController
@RequestMapping("/api/operation-types")
@Validated
@Slf4j
public class OperationTypeController {

    //postman 测试完成
    @Autowired
    private OperationTypeMapper operationTypeMapper;

    @PostMapping
    public ResponseResult<Short> createOperationType(
            @Validated @RequestBody OperationType operationType) {
        if (operationTypeMapper.insertOperationType(operationType) <= 0) {
            throw new BusinessException("操作类型创建失败");
        }
        return ResponseResult.success(operationType.getTypeId());
    }

    @GetMapping("/{id}")
    public ResponseResult<OperationType> getOperationType(
            @PathVariable @Min(1) Short id) {
        OperationType type = operationTypeMapper.selectById(id);
        if (type == null) {
            throw new BusinessException(404, "操作类型不存在");
        }
        return ResponseResult.success(type);
    }

    @PutMapping("/{id}")
    public ResponseResult<Void> updateOperationType(
            @PathVariable @Min(1) Short id,
            @Validated @RequestBody OperationType operationType) {
        operationType.setTypeId(id);
        if (operationTypeMapper.updateOperationType(operationType) <= 0) {
            throw new BusinessException("操作类型更新失败");
        }
        return ResponseResult.success();
    }

    @DeleteMapping("/{id}")
    public ResponseResult<Void> deleteOperationType(
            @PathVariable @Min(1) Short id) {
        if (operationTypeMapper.deleteOperationType(id) <= 0) {
            throw new BusinessException("操作类型删除失败");
        }
        return ResponseResult.success();
    }
}